import { Message } from 'element-ui';
import Router from '@/router';

const user = {
  namespaced: true,
  state: {
    token: localStorage.getItem('user_token'),
  },
  mutations: {
    // 退出登录
    logout(state, payload) {
      state.token = '';
      localStorage.removeItem('user_token');
      Message.success('退出成功');
      setTimeout(() => {
        Router.push('/login');
      }, 1000);
    },
    // 用户登录同时保存登录凭证
    saveInfo(state, payload) {
      // 前端时间戳token
      const customToken = Date.now();
      state.token = customToken;
      localStorage.setItem('user_token', customToken);
      Message.success('登录成功');
      setTimeout(() => {
        Router.push('/');
      }, 1000);
    },
  },
  actions: {},
};

export default user;
